function tap_to_jump (event){
    wx.redirectTo({
        url:event.currentTarget.dataset.jump_to,
        fail:function(){
            wx.switchTab({
                url:event.currentTarget.dataset.jump_to,
            })
        }
    })
}

function my_request (options){
    if(!options.fatal){
        options.fatal = function(){}
    }
    if(!options.method){
        options.method="GET"
    }
    if(!options.header){
        options.header={}
    }
    if(options.autoReq === undefined){
        options.autoReq = true
    }

    var auth_token = wx.getStorageSync("auth_token");

    if (auth_token) {
        options.header["authorization"]="Bear "+auth_token
    }

    if (!auth_token && options.autoReq) {
        let lock = wx.getStorageSync('forbid-lock')
        if(lock){
            return
        } else {
            wx.setStorageSync('forbid-lock',true)
        }
        let curUrl = `/${curUrlWithArgs()}`
        curUrl = encodeURIComponent(curUrl)
        wx.redirectTo({
            url: `/pages/login/login?next=${curUrl}`,
        })
        return
    }

    if(!options.success){
        options.success=function(){}
    }
    if(!options.fail){
        options.fail = function(){}
    }
    else{
        var old_success = options.success;
        options.success = function(responce){
            if(responce.statusCode<400&&responce.data.code=="OK"){
                //在这里保存token
                if(responce.data.data.token){
                    wx.setStorageSync('auth_token', responce.data.data.token);
                }
                old_success(responce)
            }
            else{
                options.fail(responce)
            }
        }
    }
    if(!options.complete){
        options.complete=function(){}
    }
    if(!options.data){
        options.data={}
    }

    wx.request({
        url:options.url,
        method:options.method,
        header:options.header,
        dataType:options.dataType,
        success:options.success,
        fail:options.fatal,
        complete:options.complete,
        data:options.data
    })
}

function my_showToast_error(error_str,time){
    wx.showToast({
        title: error_str,
        icon: 'success',
        image:"/icon/error.png",
        duration: time
    })
}
function my_showToast_ok(ok_str,time){
    wx.showToast({
        title: ok_str,
        icon: 'success',
        duration: time
    })
}

function this_year() {
    var date = new Date();
    var res= date.getFullYear()+"-01-01";
    return res;
}
function next_year(){
    var date = new Date();
    var res= (date.getFullYear()+1)+"-01-01";
    return res;
}

function gen_query_str(query_data){
    var contact_str=[]
    for(var each in query_data){
        contact_str.push(each+'='+query_data[each])
    }
    if(contact_str.length){
        return "?"+contact_str.join('&')
    }
    return ""
}

/*获取当前页url*/
function currentUrl(){
    var pages = getCurrentPages()
    var currentPage = pages[pages.length-1]
    return currentPage.route
}

/*获取当前页带参数的url*/
function curUrlWithArgs(){
    var pages = getCurrentPages()
    var currentPage = pages[pages.length-1]
    var url = currentPage.route
    var options = currentPage.options
    var urlWithArgs = url + '?'
    for(var key in options){
        var value = options[key]
        urlWithArgs += key + '=' + value + '&'
    }
    urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length-1)
    return urlWithArgs
}

module.exports = {
    tap_to_jump: tap_to_jump,
    my_request:my_request,
    my_showToast_error:my_showToast_error,
    my_showToast_ok:my_showToast_ok,
    this_year:this_year,
    next_year:next_year,
    gen_query_str:gen_query_str
}

